﻿// Extend the previous program to support also subtraction and multiplication of polynomials.

using System;

class Program
{
    static void Main()
    {
        decimal[] firstPolynomial = AddPolynomialsProgram.ReadPolynomial();
        Console.Write("You have enter a first polynomial: ");
        AddPolynomialsProgram.PrintPolynomial(firstPolynomial);

        decimal[] secondPolynomial = AddPolynomialsProgram.ReadPolynomial();
        Console.Write("You have enter a second polynomial: ");
        AddPolynomialsProgram.PrintPolynomial(secondPolynomial);

        Console.Write("The subtract of the polynomials is: ");
        decimal[] polynomialSubtract = SubtractPolynomials(firstPolynomial, secondPolynomial);
        AddPolynomialsProgram.PrintPolynomial(polynomialSubtract);

        Console.Write("The multiplication of polynomials is: ");
        decimal[] polynomialMultiplication = MultiplyPolinomials(firstPolynomial, secondPolynomial);
        AddPolynomialsProgram.PrintPolynomial(polynomialMultiplication);
    }

    public static decimal[] SubtractPolynomials(decimal[] firstPol, decimal[] secondPol)
    {
        int maxLength = Math.Max(firstPol.Length, secondPol.Length);
        decimal[] result = new decimal[maxLength];
        for (int i = firstPol.Length - 1; i >= 0; i--)
        {
            if (i < firstPol.Length && i < secondPol.Length)
            {
                result[i] = firstPol[i] - secondPol[i];
            }
            else if (i < firstPol.Length)
            {
                result[i] = firstPol[i];
            }
            else
            {
                result[i] = secondPol[i];
            }
        }

        return result;
    }

    private static decimal[] MultiplyPolinomials(decimal[] firstPolynomial, decimal[] secondPolynomial)
    {
        int resultSize = firstPolynomial.Length + secondPolynomial.Length - 1;
        decimal[] result = new decimal[resultSize];
        for (int i = 0; i < firstPolynomial.Length; i++)
        {
            for (int j = 0; j < secondPolynomial.Length; j++)
            {
                result[i + j] += firstPolynomial[i] + secondPolynomial[j];
            }
        }

        return result;
    }
}